package ph.com.isla.sf2d.ui;

import ph.com.isla.sf2d.IDGenerator;
import ph.com.isla.sf2d.SF2DSurfaceView;
import ph.com.isla.sf2d.ui.SF2DObjectInterface.OnClickListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.SurfaceHolder;

public abstract class SF2DObject {
	private int mId;
	private Rect mDimension = new Rect();
	private boolean mEnabled;
	private SF2DSurfaceView mRoot;
	private int mLeft;
	private int mTop;
	private int mBottom;
	private int mRight;
	private int mWidth;
	private int mHeight;
	private boolean mVisible;
	private OnClickListener mOnClickListener;

	// default values
	int mDefaultTextSize = 24;
	int mDefaultTextColor = Color.BLACK;
	int mDefaultWidth;
	int mDefaultHeight;
	int mDefaultTop;
	int mDefaultLeft;
	int mDefaultBorderColor = Color.DKGRAY;
	int mDefaultBorderWidth = 2;
	int mDefaultBackgroundColor = Color.LTGRAY;

	// Object properties
	private SF2DBorder mBorder;
	private Paint mBackgroundPaint;
	private int mBackgroundColor;

	public void setBorder(SF2DBorder border) {
		mBorder = border;
		if (mBorder != null) {
			mBorder.setSF2DObject(this);
		}
	}

	public void setBackgroundColor(int color) {
		mBackgroundColor = color;
		if (mBackgroundPaint == null)
			mBackgroundPaint = new Paint();

		mBackgroundPaint.setColor(mBackgroundColor);
		mBackgroundPaint.setStyle(Style.FILL);
	}

	public SF2DBorder getBorder() {
		return mBorder;
	}

	public void draw(Canvas canvas) {
		canvas.drawRect(mDimension, mBackgroundPaint);
		if (mBorder != null) 
			mBorder.draw(canvas);
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		mOnClickListener = onClickListener;
	}

	public OnClickListener getOnClickListener() {
		return mOnClickListener;
	}

	public SF2DSurfaceView getSurface() {
		return mRoot;
	}

	public boolean isVisible() {
		return mVisible;
	}

	public void setVisible(boolean visible) {
		mVisible = visible;
	}

	public boolean isEnabled() {
		return mEnabled;
	}

	public void setEnabled(boolean enabled) {
		mEnabled = enabled;
	}

	public void setTop(int top) {
		mTop = top;
	}

	public void setLeft(int left) {
		mLeft = left;
	}

	public Rect getDimension() {
		mDimension.set(mLeft, mTop, mLeft + mWidth, mTop + mHeight);
		return mDimension;
	}
	
	public Rect getRect() {
		return new Rect((int) mLeft, (int) mTop, (int) (mLeft + mWidth),
				(int) (mTop + mHeight));
	}

	void setWidth(int width) {
		mWidth = width;
	}

	void setHeight(int height) {
		mHeight = height;
	}

	public SF2DObject(SF2DSurfaceView root) {
		this.mRoot = root;

		mId = IDGenerator.generateId();
		setBackgroundColor(mDefaultBackgroundColor);
	}

	public int getId() {
		return mId;
	}

	public abstract void onKeyDown(float x, float y);

	public abstract void onKeyUp(float x, float y);

	public abstract void onMoveOut(float x, float y);

	public abstract boolean isClickable();
	
	public abstract Rect getClickableArea();

	public boolean isPointInsideClickableArea(int x, int y) {
		return mDimension.contains(x, y);
	}

}
